package com.hooper.controller;

import com.hooper.common.Result;
import com.hooper.model.dto.SysMenuDto;
import com.hooper.model.system.SysMenu;
import com.hooper.model.vo.AssignMenuVo;
import com.hooper.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/04/01/21:01
 */
@Api(tags = "系统菜单管理")
@RequestMapping("admin/system/menu")
@RestController
public class SysMenuController {

    @Resource
    private SysMenuService menuService;

    //获取所有菜单
    @ApiOperation("获取所有菜单")
    @GetMapping("allMenus")
    public Result<List<SysMenuDto>> getAllMenus(){
        return Result.ok(menuService.getAllMenus());
    }

    @ApiOperation("更新菜单")
    @PutMapping("update")
    public Result<Void> update(@RequestBody SysMenu newMenu) {
        menuService.updateMenuById(newMenu);
        return Result.ok();
    }

    @ApiOperation("新增菜单")
    @PostMapping("save")
    public Result<Void> save(@RequestBody SysMenu menu) {
        menuService.saveMenu(menu);
        return Result.ok();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    public Result<Void> delById(@PathVariable Long id) {
        menuService.delMenuById(id);
        return Result.ok();
    }

    @ApiOperation("根据角色id获取菜单")
    @GetMapping("getMenusByRoleId/{roleId}")
    public Result<List<SysMenuDto>> getMenusByRoleId(@PathVariable Long roleId) {
        return Result.ok(menuService.getMenusByRoleId(roleId));
    }

    @ApiOperation("根据角色id分配菜单")
    @PostMapping("assignMenu2Role")
    public Result<Object> assignMenu2Role(@RequestBody AssignMenuVo assignMenuVo) {
        menuService.assignMenu2Role(assignMenuVo);
        return Result.ok();
    }
}
